package com.atguigu.eduservice.service.impl;

import com.atguigu.commonutils.R;
import com.atguigu.eduservice.client.VodClient;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.vo.VideoInfo;
import com.atguigu.eduservice.mapper.EduVideoMapper;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author 老王
 * @since 2020-05-20
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    @Autowired
    private VodClient vodClient;
    //保存小节
    @Override
    public void saveVideoInfo(VideoInfo videoInfo) {
        EduVideo video = new EduVideo();
        BeanUtils.copyProperties(videoInfo, video);
        boolean result = this.save(video);
        if(!result){
            throw new GuliException(20001, "课时信息保存失败");
        }
    }

    //修改小节信息
    @Override
    public void updateVideoInfo(VideoInfo videoInfo) {
        EduVideo video = new EduVideo();
        BeanUtils.copyProperties(videoInfo, video);
        boolean result = this.updateById(video);
        if(!result){
            throw new GuliException(20001, "课时信息修改失败");
        }
    }

    //根据id删除小节同时调用vod服务删除视频
    @Override
    public boolean removeVideoById(String videoId) {
        //先查出视频的id
        EduVideo video = this.getById(videoId);
        String videoSourceId = video.getVideoSourceId();
        if(!StringUtils.isEmpty(videoSourceId)){{
            //调用服务删除
            R r = vodClient.removeById(videoSourceId);
            if (r.getCode()!=20000){
                throw new GuliException(200001, "删除课时失败");
            }
        }}
        //再删数据库中的小节
        return this.removeById(videoId);
    }

    //根据id查询小节
    @Override
    public EduVideo getVideoById(String id) {
        return this.getById(id);
    }

    //根据课程id删除小结
    @Override
    public boolean removeByCourseId(String courseId) {
        QueryWrapper<EduVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id", courseId);
        List<EduVideo> videoList = baseMapper.selectList(queryWrapper);

        //得到所有属于该课程的视频id
        List<String> videoSourceIdList = new ArrayList<>();
        for (int i = 0; i < videoList.size(); i++) {
            EduVideo video = videoList.get(i);
            String videoSourceId = video.getVideoSourceId();
            if(!StringUtils.isEmpty(videoSourceId)){
                videoSourceIdList.add(videoSourceId);
            }
        }

        //调用服务删除
        if(videoSourceIdList.size() > 0){
            R res = vodClient.removeVideiList(videoSourceIdList);
            if (res.getCode()==20001){
                throw new GuliException(20001, "删除视频出错");
            }
        }
        QueryWrapper<EduVideo> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("course_id", courseId);
        Integer count = baseMapper.delete(queryWrapper1);
        return null != count && count > 0;
    }

}
